package demo.practice.leetcode_eveyday;

import java.util.Arrays;
import java.util.LinkedList;

public class P239 {


    public int[] maxSlidingWindow(int[] nums, int k) {

            if(nums==null||nums.length<=1)
                return nums;

          /*  if(k>nums.length){
                int m1=nums[0];
                for(int i=1;i<nums.length;i++){
                    if(nums[i]>m1)
                        m1=nums[i];
                }
                return new int[]{m1};
            }*/

            int target[]= new int[nums.length-k+1];

            LinkedList<Integer> queue = new LinkedList();
            for(int i=0;i<nums.length;i++){
                while (!queue.isEmpty()&&nums[queue.peekLast()]<=nums[i]){
                    queue.pollLast();
                }

                queue.addLast(i);
                if(queue.peek()<i-k)
                    queue.poll();

                if(i+1>=k)
                    target[i+1-k]=nums[queue.peek()];
            }

            return target;
    }



}
